//
//  RingSubscribeDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/18.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "EvaluateOrderViewController.h"
#import "RingSubscribeDetailViewController.h"

@interface RingSubscribeDetailViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *storeIcon;
@property (weak, nonatomic) IBOutlet UILabel *storeName;
@property (weak, nonatomic) IBOutlet UILabel *statusLab;


@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;

@property (weak, nonatomic) IBOutlet UILabel *manSizeLab;
@property (weak, nonatomic) IBOutlet UILabel *womanSizeLab;

@property (weak, nonatomic) IBOutlet UILabel *recCode;
@property (weak, nonatomic) IBOutlet UILabel *ctimeLab;

@property (weak, nonatomic) IBOutlet UIButton *actionBtn;

@property (nonatomic, strong) CommonSubscribeRingModel *model;

@end

@implementation RingSubscribeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"预约详情";
    self.actionBtn.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self request_ApplyDetail];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.actionBtn pp_clipCorners:UIRectCornerAllCorners radius:self.actionBtn.frame.size.height/2];
        [self.storeIcon pp_clipCorners:UIRectCornerAllCorners radius:self.storeIcon.frame.size.height/2];
    });
}

- (void)fillData {
    
    [self.storeIcon sd_setImageWithURL:[NSURL URLWithString:self.model.logoUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.storeName.text = self.model.sellerName;

    self.nameLab.text = self.model.name;
    self.phoneLab.text = self.model.phone;
    self.manSizeLab.text = self.model.manSize;
    self.womanSizeLab.text = self.model.girlSize;
//    self.recCode.text = self.model.recommendCode;
    self.ctimeLab.text = self.model.createTime;
    
    ////领取状态1待领取2已领取3已取消
    switch (self.model.type) {
        case 1:{
            self.statusLab.text = @"待领取";
            self.statusLab.textColor = UIColorHex(0xff7200);
            self.actionBtn.hidden = NO;
            
            switch (self.model.isReceive) {
                case 1:{
                    self.actionBtn.hidden = NO;
                    [self.actionBtn setTitle:@"确认领取" forState:UIControlStateNormal];
                } break;
                case 2:{
                    self.actionBtn.hidden = YES;
                } break;
                default:
                    break;
            }
            
        } break;
        case 2:{
            self.statusLab.text = @"已领取";
            self.statusLab.textColor = UIColorHex(0x333333);
            
            //是否评价1：是2：否
            switch (self.model.isEvaluate) {
                case 1:{
                    self.actionBtn.hidden = YES;
                } break;
                case 2:{
                    self.actionBtn.hidden = NO;
                    [self.actionBtn setTitle:@"去评价" forState:UIControlStateNormal];
                } break;
                default:
                    break;
            }
        } break;
        case 3:{
            self.actionBtn.hidden = YES;
            self.statusLab.text = @"已取消";
            self.statusLab.textColor = UIColorHex(0x999999);
        } break;
        default:{
            self.actionBtn.hidden = YES;
            self.statusLab.text = @"未知状态";
        } break;
    }
}

- (IBAction)handleAction:(id)sender {
    
    ////领取状态1待领取2已领取3已取消
    switch (self.model.type) {
        case 1:{//领取手环
            [self request_cacheRing];
        } break;
        case 2:{//去评价

            EvaluateOrderViewController *vc = [[EvaluateOrderViewController alloc] init];
            vc.subscribeRingModel = self.model;
            [self.navigationController pushViewController:vc animated:YES];
            
        } break;
        default:{

        } break;
    }
}

- (void)request_ApplyDetail {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ApplyRingDetail];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.abId forKey:@"abId"];
    [SVProgressHUD showWithStatus:@"正在加载"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                weakSelf.model = [CommonSubscribeRingModel modelWithDictionary:resultDic];
                [weakSelf fillData];
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

//请求获取手环
//请求获取手环
- (void)request_cacheRing {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_RingUpdate];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.model.abId forKey:@"abId"];
    [dic setValue:@"2" forKey:@"type"];

    [SVProgressHUD showWithStatus:@"正在提交"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];

                [weakSelf request_ApplyDetail];
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}



@end
